package com.itfreer.ui.advanced;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.itfreer.ui.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 定义圆形切换控件
 */
public class SwitchButtonView extends LinearLayout {

    // 需要显示的
    private String showTitles;
    // 当项改变时
    private OnSwitchListener onSwitchListener;
    private List<TextView> items;

    /**
     * 当项改变时事件接口
     */
    public interface OnSwitchListener {
        void onSwitch(View e, int index);
    }

    public void setOnSwitchListener(OnSwitchListener onSwitchListener){
        this.onSwitchListener = onSwitchListener;
    }

    public SwitchButtonView(Context context) {
        super(context);
    }

    public SwitchButtonView(Context context, AttributeSet attrs){
        super(context, attrs);
        getAttrs(context, attrs);
        setView(context);
    }

    public SwitchButtonView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        getAttrs(context, attrs);
        setView(context);
    }

    protected void getAttrs(Context context, AttributeSet attrs){
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SimpleView);
        showTitles = a.getString(R.styleable.SimpleView_showTitles);
    }

    /**
     * 只更新显示标题，不处理其它事项
     * @param title
     */
    public void updateTitles(String title){
        showTitles = title;
        if(items==null){
            return;
        }
        String[] titles = showTitles.split(",");
        if(items.size()!=titles.length){
            return;
        }
        for(int i=0;i<titles.length;i++){
            items.get(i).setText(titles[i]);
        }
    }

    /**
     * 设置当前的视图
     * @param context
     */
    protected void setView(Context context){

        // 设置基础背景
        this.setBackgroundResource(R.drawable.com_itfreer_core_ui_advanced_switchbuttonview_corners_all);
        this.setGravity(Gravity.CENTER);

        items = new ArrayList<>();
        // 添加项
        if(showTitles==null || showTitles==""){
            return;
        }

        String[] titles = showTitles.split(",");
        int i=0;
        for(String title : titles){
            TextView item = new TextView(context);
            // 默认项
            if(i==0){
                item.setTextColor(Color.WHITE);
                item.setBackgroundResource(R.drawable.com_itfreer_core_ui_advanced_switchbuttonview_corners_checked);
            }else{
                item.setTextColor(Color.BLACK);
                item.setBackgroundResource(R.drawable.com_itfreer_core_ui_advanced_switchbuttonview_corners_unchecked);
            }
            item.setText(title);
            item.setTextSize(20);
            item.setTag(i);
            item.setGravity(Gravity.CENTER);
            LayoutParams param =
                    new LayoutParams(0, LayoutParams.FILL_PARENT);
            param.weight = 1;
            param.setMargins(2,2,2,2);
            items.add(item);
            this.addView(item, param);

            item.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View v) {
                    for (TextView item : items) {
                        if (v == item) {
                            item.setTextColor(Color.WHITE);
                            item.setBackgroundResource(R.drawable.com_itfreer_core_ui_advanced_switchbuttonview_corners_checked);
                        } else {
                            item.setTextColor(Color.BLACK);
                            item.setBackgroundResource(R.drawable.com_itfreer_core_ui_advanced_switchbuttonview_corners_unchecked);
                        }
                    }
                    if (onSwitchListener != null) {
                        int index = (int) v.getTag();
                        onSwitchListener.onSwitch(v, index);
                    }
                }
            });
            i++;
        }
    }
}